package indi.xielin.start.controller;

import com.baomidou.mybatisplus.core.metadata.IPage;
import indi.xielin.start.domain.dto.CarDTO;
import indi.xielin.start.mapper.CarMapper;
import indi.xielin.start.page.MyPage;
import indi.xielin.start.service.CarService;
import lombok.extern.slf4j.Slf4j;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.*;

import java.math.BigDecimal;
import java.util.Collection;
import java.util.Collections;
import java.util.List;

@RestController
@Slf4j
public class CarController {
    @Autowired
    private CarService carService;
    @Autowired
    private CarMapper carMapper;

    @RequestMapping("/car/select")
    public List<CarDTO> select(){
//        System.out.println(carMapper.deleteAll());
        List<CarDTO> carDTOList = carService.getBaseMapper().selectList(null);
        carDTOList.forEach(System.out::println);
        return carDTOList;
    }

    @RequestMapping("/car/delete/{id}")
    private boolean deleteById(@PathVariable Long id){
        return carService.getBaseMapper().deleteById(id)>0?true:false;
    }

    @RequestMapping("/car/deleteAll")
    private boolean deleteAll(){
        return carMapper.deleteAll()>0?true:false;
    }

    @RequestMapping("/car/update")
    private boolean updateById(@RequestBody CarDTO carDTO){
        return carService.getBaseMapper().updateById(carDTO)>0?true:false;
    }

    @RequestMapping("/car/updateAll")
    private int update(@RequestBody CarDTO carDTO){
        return carService.getBaseMapper().update(carDTO,null);
    }

    @RequestMapping("/car/update/name")
    private int updateByName(@RequestParam String name){
        return carMapper.updateByName(name);
    }

    @RequestMapping("/car/select/page")
    private int selectPage(){
        MyPage<CarDTO> dtoMyPage = new MyPage<>();
        dtoMyPage.setCurrent(3L);//需要第三页
        dtoMyPage.setSize(2L);//每页两条数据      第三页开始号为4（0，1），（2，3），（4...）  limit (4,2)
        MyPage<CarDTO> myPage = carService.getBaseMapper().selectPage(dtoMyPage, null);
        myPage.getRecords().iterator().forEachRemaining(System.out::println);
        System.out.println("当前页大小"+myPage.getSize());
        System.out.println("当前页"+myPage.getCurrent());
        System.out.println("所有数据总数"+myPage.getTotal());
//        myPage.getRecords();获取数据
        return 0;
    }
}
